import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';

///
/// ProjectName: platform_service
/// Author: lizhi
/// CreateDate: 2022/12/27 18:00
/// Copyright: ©2022 NEW CORE Technology Co. Ltd. All rights reserved.
/// Description: 代码文件说明，markdown 格式
///

/// 文件选择支持的 格式（文件后缀名列表），新增选项时 [NDFileInfo] 的mimeType需要相应处理

const allowedExtensions = [
  'png',
  'jpg',
  'jpeg',
  'gif',
  'txt',
  'xls',
  'xlsx',
  'doc',
  'docx',
  'ppt',
  'pptx',
  'pdf',
  'zip',
  'rar',
  '7z',
  'gz',
  'tar',
  'key',
  'rtf',
  'csv',
  'xps',
];

class NDFilePickerResult {
  NDFilePickerResult(
    List<PlatformFile> selectFiles,
  ) {
    for (var pFile in selectFiles) {
      var file = NDFileInfo(
        name: pFile.name,
        size: pFile.size,
        path: kIsWeb ? null : pFile.path,
        fileBytes: kIsWeb ? pFile.bytes?.toList() ?? <int>[] : null,
      );
      if (allowedExtensions.contains(file.extension)) {
        files.add(file);
      } else {
        unSupportFiles.add(file);
      }
    }
  }

  /// 选中的文件列表
  final List<NDFileInfo> files = [];

  /// 不支持的文件列表
  final List<NDFileInfo> unSupportFiles = [];
}

class NDFileInfo {
  NDFileInfo({
    this.path,
    required this.name,
    required this.size,
    this.fileBytes,
    String? mimeType,
  }) {
    _mimeType = mimeType;
  }

  factory NDFileInfo.fromMap(Map data, {List<int>? fileBytes}) {
    return NDFileInfo(
      name: data['name'],
      path: data['path'],
      size: data['size'],
      fileBytes: fileBytes,
    );
  }

  /// On web this is always `null`. You should access `bytes` property instead.
  String? path;

  /// File name including its extension.
  final String name;

  /// Byte data for this file. Particularly useful if you want to manipulate its data
  /// or easily upload to somewhere else.
  final List<int>? fileBytes;

  /// The file size in bytes. Defaults to `0` if the file size could not be
  /// determined.
  final int size;

  /// File extension for this file.
  String? get extension => name.split('.').last;

  /// 手机端 拍照、相册图片时赋值
  String? _mimeType;

  String? get mimeType {
    var extensionText = extension;
    if (_mimeType != null) {
      return _mimeType;
    } else if (['png', 'jpg', 'jpeg', 'gif'].contains(extensionText)) {
      if (extensionText == 'jpg') {
        return 'image/jpeg';
      }
      return 'image/$extensionText';
    } else if (['pdf'].contains(extensionText)) {
      return 'application/pdf';
    } else if (['txt', 'rtf', 'key'].contains(extensionText)) {
      return 'text/plain';
    } else if (['doc', 'docx'].contains(extensionText)) {
      return 'application/msword';
    } else if (['xls', 'xlsx'].contains(extensionText)) {
      return 'application/vnd.ms-excel';
    } else if (['ppt', 'pptx'].contains(extensionText)) {
      return 'application/vnd.ms-powerpoint';
    } else if (['zip', 'rar', '7z', 'gz', 'tar'].contains(extensionText)) {
      return 'application/zip';
    } else if (['csv'].contains(extensionText)) {
      return 'text/csv';
    } else if (['xps'].contains(extensionText)) {
      return 'application/vnd.ms-xpsdocument';
    }
    return '';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) {
      return true;
    }

    return other is NDFileInfo &&
        (kIsWeb || other.path == path) &&
        other.name == name &&
        other.fileBytes == fileBytes &&
        other.size == size;
  }

  @override
  int get hashCode {
    return kIsWeb
        ? 0
        : path.hashCode ^ name.hashCode ^ fileBytes.hashCode ^ size.hashCode;
  }

  @override
  String toString() {
    return 'PlatformFile(${kIsWeb ? '' : 'path $path'}, name: $name, readStream: $fileBytes, size: $size)';
  }
}
